package io.github.talelin.latticy.controller.api;

import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.common.UserUtil;
import io.github.talelin.latticy.dto.VacationDTO;
import io.github.talelin.latticy.service.VacationService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.GroupIsUserVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/api/vacation")
@Validated
public class VacationController {

    @Autowired
    private VacationService vacationService;

    @Autowired
    private UserUtil userUtil;

    /**
     * 审批人员列表
     * @return
     */
    @GetMapping("/user")
    @LoginRequired
    public List<GroupIsUserVO> user() {
        return userUtil.getUsers();
    }

    /**
     * 请假
     * @param dto
     * @return
     */
    @PostMapping
    @LoginRequired
    public CreatedVO create(@RequestBody @Validated VacationDTO dto) {
        vacationService.create(dto);
        return new CreatedVO<>();
    }

    /**
     * 编辑请假
     */
    @PutMapping("/{id}")
    @LoginRequired
    public UpdatedVO update(@RequestBody @Validated VacationDTO dto,
                       @PathVariable @Positive Long id) {
        vacationService.updated(id, dto);
        return new UpdatedVO<>();
    }
}
